#####################
### Load Packages ###
#####################

library(haven)


# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")


SummaryTable <- function(CurrentVariable, VariableName)
{
  CurrentTable <- data.frame(matrix(NA,1,0))
  CurrentTable$VariableName <- VariableName
  CurrentTable$Mean <- round(mean(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$Median <- round(median(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$Min <- round(min(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$Max <- round(max(CurrentVariable, na.rm=TRUE),2)
  CurrentTable$SD <- round(sd(CurrentVariable, na.rm=TRUE),2)
  return(CurrentTable)
}

####
# Congress
####

####
# Stauffer
####

StaufferCongress <- read_dta("Data/Formatted Data/Stauffer-Congress-RegressionData.dta")

CurrentTable <- SummaryTable(StaufferCongress$efficacy_1, "External Efficacy")
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$r_estimate, "Estimate % Women"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$gender_r, "Female"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$ideo7, "Ideology"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$Republican, "Republican"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$Independent, "Independent"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$strong_partisan, "Strong Partisan"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$know_fed, "Federal Knowledge"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$religiosity, "Religiosity"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$coll, "College Degree"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$white, "White"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$age, "Age"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$employed, "Employed"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$woman_sen, "Woman Senator"))
CurrentTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$woman_rep, "Woman Rep"))
StaufferFullTable <- rbind(CurrentTable, SummaryTable(StaufferCongress$stateleg, "Woman State Leg"))


####
# Clark-Rogers
####

ClarkRogersData <- read_dta("Data/Formatted Data/Clark-Rogers-RegressionData.dta")

CurrentTable <- SummaryTable(ClarkRogersData$ExternalEfficacy_Congress, "External Efficacy")
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Trust_Congress_Pre, "Trust in Congress"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Belief_WomenCongress, "Estimate % Women"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Female, "Female"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Ideology, "Ideology"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Republican, "Republican"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Independent, "Independent"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$StrongPartisan, "Strong Partisan"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$FederalKnowledge, "Federal Knowledge"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Religiosity, "Religiosity"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$CollegeDegree, "College Degree"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$White, "White"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Age, "Age"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Employed, "Employed"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$USSenator_Female, "Woman Senator"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$MoC_Female, "Woman Rep"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$True_WomenStateLeg, "Woman State Leg"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$RR_Tesler, "Racial Resentment (Tesler)"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$RR_Kinder, "Racial Resentment (Kinder & Sanders)"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Fem_Tesler, "Sexism"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Black, "Black"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$Belief_BlackCongress, "Estimate % Black"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$USSenator_Black, "Black US Senator"))
CurrentTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$MoC_Black, "US Senator Black"))
RogersTable <- rbind(CurrentTable, SummaryTable(ClarkRogersData$True_BlackStateLeg, "Black State Leg"))
RogersTable$Order <- seq(1,nrow(RogersTable))

CombinedTable <- merge(RogersTable, StaufferFullTable, by.x="VariableName", by.y="VariableName", all.x=TRUE)

CombinedTable <- CombinedTable[order(CombinedTable$Order),]
CombinedTable$Order <- NULL

colnames(CombinedTable) <- c("Variable", "Mean", "Median", "Min", "Max", "SD", "Mean", "Median", "Min", "Max", "SD")

write.csv(CombinedTable, "Tables/Appendix-Table-A-6.csv", row.names=FALSE)
